package com.hlyj.http.base.tool.lib_hlyj_base.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.ProxyInfo;
import android.os.Build;

public class ProxyChecker{

    public static String checkProxyStatus(Context context) {
        return isProxyEnabled(context) ? "1" : "0";
    }

    private static boolean isProxyEnabled(Context context) {
        // 方法1：使用官方 API 检测（Android 5.0+）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm != null) {
                Network activeNetwork = cm.getActiveNetwork();
                if (activeNetwork != null) {
                    LinkProperties lp = cm.getLinkProperties(activeNetwork);
                    if (lp != null) {
                        ProxyInfo proxyInfo = lp.getHttpProxy();
                        if (proxyInfo != null) {
                            String host = proxyInfo.getHost();
                            int port = proxyInfo.getPort();
                            return host != null && !host.isEmpty() && port != -1;
                        }
                    }
                }
            }
        }

        // 方法2：检查系统属性（适用于所有版本）
        String proxyHost = System.getProperty("http.proxyHost");
        String proxyPort = System.getProperty("http.proxyPort");
        return proxyHost != null && !proxyHost.isEmpty()
                && proxyPort != null && !proxyPort.isEmpty();
    }

//    private static boolean isWifiProxyEnabled(Context context) {
//        try {
//            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//
//            Method getHttpProxyMethod = wifiInfo.getClass().getMethod("getHttpProxy");
//            Object proxyInfo = getHttpProxyMethod.invoke(wifiInfo);
//
//            if (proxyInfo != null) {
//                Method getHostMethod = proxyInfo.getClass().getMethod("getHost");
//                Method getPortMethod = proxyInfo.getClass().getMethod("getPort");
//
//                String host = (String) getHostMethod.invoke(proxyInfo);
//                int port = (Integer) getPortMethod.invoke(proxyInfo);
//
//                return host != null && !host.isEmpty() && port != -1;
//            }
//        } catch (Exception e) {
//            // 忽略异常
//        }
//        return false;
//    }
}